﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="XmlSerializerHelper.cs" company="SMART business">
//   Copyright (c) 2014 All Rights Reserved   
// </copyright>
// <summary>
//   The xml serializer helper.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ComparisonTool
{
    using System;
    using System.IO;
    using System.Text;
    using System.Xml.Serialization;

    /// <summary>
    /// The xml serializer helper.
    /// </summary>
    public class XmlSerializerHelper
    {
        /// <summary>
        /// The deserialize function.
        /// </summary>
        /// <param name="xmlContent">
        /// The xml content.
        /// </param>
        /// <param name="objectType">
        /// The object for serialization.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public static object Deserialize(string xmlContent, Type objectType)
        {
            try
            {
                var reader = new StringReader(xmlContent);
                var serializer = new XmlSerializer(objectType);
                return serializer.Deserialize(reader);
            }
            catch (Exception error)
            {
                throw new Exception("Error while deserializing: " + error.Message);
            }
        }

        /// <summary>
        /// The serialize.
        /// </summary>
        /// <param name="o">
        /// The o.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string Serialize(object o)
        {
            try
            {
                var serializer = new XmlSerializer(o.GetType());
                var stringBuilder = new StringBuilder();
                using (var writer = new StringWriter(stringBuilder))
                {
                    serializer.Serialize(writer, o);
                }

                return stringBuilder.ToString();
            }
            catch (Exception error)
            {
                throw new Exception("Error while serializing: " + error.Message);
            }
        }

        /// <summary>
        /// The serialize to file.
        /// </summary>
        /// <param name="o">
        /// The o.
        /// </param>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <exception cref="Exception">
        /// </exception>
        public static void SerializeToFile(object o, string path)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(o.GetType());

                using (var streamWriter = new StreamWriter(path, false))
                {
                    xmlSerializer.Serialize(streamWriter, o);
                }
            }
            catch (Exception error)
            {
                throw new Exception("Error while serializing: " + error.Message);
            }
        }
    }
}